package com.loe.ui.api

import android.webkit.JavascriptInterface
import com.loe.mvp.ext_view.addTextAfterListener
import com.loe.mvp.ext_view.string
import com.loe.ui.*
import com.loe.ui.view.UiTextView
import com.quickjs.JSArray
import com.quickjs.JSFunction
import com.quickjs.JSObject
import com.quickjs.JavaCallback

open class TextViewApi(private val view: UiTextView) : ViewApi(view)
{
    var text: String
        @JavascriptInterface get() = view.string
        @JavascriptInterface set(value)
        {
            view.setAutoText(value)
        }

    @JavascriptInterface
    fun addTextListener(callback: JSFunction)
    {
        view.addTextAfterListener { callback.call(jsObject, JSArray(jsContext).push(it)) }
    }

    @JavascriptInterface
    fun setTextListener(callback: JSFunction)
    {
        view.setTextListener { callback.call(jsObject, JSArray(jsContext).push(it)) }
    }

    @JavascriptInterface
    fun setTextColor(color: String)
    {
        view.setTextColor(color.color)
    }

    @JavascriptInterface
    fun setBold(bold: Boolean)
    {
        view.setBold(bold)
    }

    @JavascriptInterface
    fun setItalic(italic: Boolean)
    {
        view.setItalic(italic)
    }

    @JavascriptInterface
    fun setTextStyle(textStyle: String)
    {
        view.setTextStyle(textStyle)
    }

    @JavascriptInterface
    fun setTextSize(size: String)
    {
        view.setTextSize(size)
    }

    @JavascriptInterface
    fun setImageColor(color: String)
    {
        view.imageColor = color
        view.notifyImage()
    }

    init
    {
        jsObject.registerFun("setImage")
        {
            view.image = it.get(0)
            view.notifyImage()
        }
    }
}